package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.navigation.NavigationView;

import cn.lrwjz.androidguide.R;

public class DrawerActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private TextView contentText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);

        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
            getSupportActionBar().setTitle("Drawer 示例");
        }

        drawerLayout = findViewById(R.id.drawerLayout);
        contentText = findViewById(R.id.contentText);
        NavigationView navigationView = findViewById(R.id.navigationView);

        // 设置导航项选中监听器
        navigationView.setNavigationItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                contentText.setText("首页内容");
            } else if (itemId == R.id.nav_dashboard) {
                contentText.setText("仪表盘内容");
            } else if (itemId == R.id.nav_notifications) {
                contentText.setText("通知内容");
            } else if (itemId == R.id.nav_settings) {
                contentText.setText("设置内容");
            }
            
            // 关闭抽屉
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;
        });

        // 设置抽屉状态监听器
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                // 抽屉滑动时的回调
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                // 抽屉打开时的回调
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                // 抽屉关闭时的回调
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                // 抽屉状态改变时的回调
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                drawerLayout.closeDrawer(GravityCompat.START);
            } else {
                drawerLayout.openDrawer(GravityCompat.START);
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
} 